/*
 * @Author: boyyang
 * @Date: 2022-01-04 16:01:29
 * @LastEditTime: 2022-01-04 17:44:05
 * @LastEditors: boyyang
 * @Description: 异步路由
 * @FilePath: \boyyang\src\store\modules\asyncRoutes.ts
 */

import { defineStore } from 'pinia'
import { store } from '@/store'
import type { RouteRecordRaw } from 'vue-router'
import { useUserStoreWithOut } from './user'
import { getAsyncRoutes } from '@/utils/getAsyncRoutes'

export interface IAsyncRoutesStore {
    asyncRoutes: Array<RouteRecordRaw>
}

const asyncRoutesStore = defineStore({
    id: 'app-asyncRoutesStore',
    state: (): IAsyncRoutesStore => ({
        asyncRoutes: []
    }),

    getters: {
        // 获取路由
        getAsyncRoutes(): Array<RouteRecordRaw> {
            return this.asyncRoutes
        }
    },

    actions: {
        async setAsyncRoutes(asyncRoutes: Array<RouteRecordRaw>) {
            const userStore = useUserStoreWithOut()
            const roles = userStore.getRoles
            const permissionRoutes = getAsyncRoutes(asyncRoutes, roles)
            this.asyncRoutes = permissionRoutes
        }
    }
})


const useAsyncRoutesStoreWithOut = () => {
    return asyncRoutesStore(store)
}

export {
    asyncRoutesStore,
    useAsyncRoutesStoreWithOut
}